package com.yygo.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/6/17.
 */
@Entity
@Table(name = "dealer")
public class Dealer extends BaseObject {

    private static final long serialVersionUID = -3680589716431065525L;
    private Long id;
    private String name;
    private String province;
    private String city;
    private String mailAddress;
    private String riskRank;
    private boolean allowNoMortgaged;
    private boolean insuranceChannel;

    private boolean plateRestricted;

    private List<User> users = new ArrayList<>();
    private User adminUser = new User();

    private BondCompany bond;

    private String bank;
    private String bankAccount;
    private String cardName;

    private BondCompany slaveProvider;
    private Long slaveProviderId;
    private BondCompany masterProvider;
    private Long masterProviderId;
    private float premiumRate;

    private boolean requestEnabled;
    private boolean loanEnabled;
    private Set<DealerRule> dealerRules = new HashSet<>();

    public Dealer() {}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(nullable = false, length = 50)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(nullable = false, length = 20)
    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    @Column(nullable = false, length = 20)
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Column(name="mail_address", nullable = false)
    public String getMailAddress() {
        return mailAddress;
    }

    public void setMailAddress(String mailAddress) {
        this.mailAddress = mailAddress;
    }

    @Column(name="risk_rank", nullable = false, length = 10)
    public String getRiskRank() {
        return riskRank;
    }

    public void setRiskRank(String riskRank) {
        this.riskRank = riskRank;
    }

    @Column(name="allow_no_mortgaged")
    public boolean isAllowNoMortgaged() {
        return allowNoMortgaged;
    }

    public void setAllowNoMortgaged(boolean allowNoMortgaged) {
        this.allowNoMortgaged = allowNoMortgaged;
    }

    @Column(name="insurance_channel")
    public boolean isInsuranceChannel() {
        return insuranceChannel;
    }

    public void setInsuranceChannel(boolean insuranceChannel) {
        this.insuranceChannel = insuranceChannel;
    }

    @Column(name="plate_restricted")
    public boolean isPlateRestricted() {
        return plateRestricted;
    }

    public void setPlateRestricted(boolean plateRestricted) {
        this.plateRestricted = plateRestricted;
    }

    @OneToMany(mappedBy = "dealer", cascade = CascadeType.REMOVE)
    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @OneToMany(mappedBy = "dealer", cascade= CascadeType.MERGE, fetch = FetchType.EAGER)
    public Set<DealerRule> getDealerRules() {
        return dealerRules;
    }

    public void setDealerRules(Set<DealerRule> dealerRules) {
        this.dealerRules = dealerRules;
    }


    @ManyToOne
    @JoinColumn(name = "bond_id")
    public BondCompany getBond() {
        return bond;
    }

    public void setBond(BondCompany bond) {
        this.bond = bond;
    }

    @OneToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name="admin_user_id")
    public User getAdminUser() {
        return adminUser;
    }

    public void setAdminUser(User adminUser) {
        this.adminUser = adminUser;
    }

    @Column(name="bank")
    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    @Column(name="bank_account")
    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    @Column(name="card_name")
    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    @ManyToOne
    @JoinColumn(name="slave_provider_id")
    public BondCompany getSlaveProvider() {
        return slaveProvider;
    }

    public void setSlaveProvider(BondCompany slaveProvider) {
        this.slaveProvider = slaveProvider;
    }

    @ManyToOne
    @JoinColumn(name="master_provider_id")
    public BondCompany getMasterProvider() {
        return masterProvider;
    }

    public void setMasterProvider(BondCompany masterProvider) {
        this.masterProvider = masterProvider;
    }
  @Transient
  public Long getSlaveProviderId() {
    return slaveProviderId;
  }

  public void setSlaveProviderId(Long slaveProviderId) {
    this.slaveProviderId = slaveProviderId;
  }

  @Transient
  public Long getMasterProviderId() {
    return masterProviderId;
  }

  public void setMasterProviderId(Long masterProviderId) {
    this.masterProviderId = masterProviderId;
  }
    @Column(name="premium_rate")
    public float getPremiumRate() {
        return premiumRate;
    }

    public void setPremiumRate(float premiumRate) {
        this.premiumRate = premiumRate;
    }

    @Column(name="request_enabled")
    public boolean isRequestEnabled() {
        return requestEnabled;
    }

    public void setRequestEnabled(boolean requestEnabled) {
        this.requestEnabled = requestEnabled;
    }

    @Column(name="loan_enabled")
    public boolean isLoanEnabled() {
        return loanEnabled;
    }

    public void setLoanEnabled(boolean loanEnabled) {
        this.loanEnabled = loanEnabled;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Dealer feedback = (Dealer) o;

        if (name.equals(feedback.name)) {
            return true;
        }

        return false;
    }

    public int hashCode() {
        int result = 3;
        result = 57 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "Dealer{" +
                "name=" + name +
                '}';
    }
}
